package com.javaxiaobear.module.monitor.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.javaxiaobear.base.common.constant.CacheConstants;
import com.javaxiaobear.base.common.utils.StringUtils;
import com.javaxiaobear.base.framework.aspectj.lang.annotation.Log;
import com.javaxiaobear.base.framework.aspectj.lang.enums.BusinessType;
import com.javaxiaobear.base.framework.redis.RedisCache;
import com.javaxiaobear.base.framework.security.LoginUser;
import com.javaxiaobear.base.framework.web.controller.BaseController;
import com.javaxiaobear.base.framework.web.domain.AjaxResult;
import com.javaxiaobear.base.framework.web.page.TableDataInfo;
import com.javaxiaobear.module.monitor.domain.SysUserOnline;
import com.javaxiaobear.module.system.service.ISysUserOnlineService;
import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 在线用户监控
 *
 * @author javaxiaobear
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {
  @Resource ISysUserOnlineService userOnlineService;

  @Resource RedisCache redisCache;
  @Resource RedissonClient redissonClient;

  @SaCheckPermission("monitor:online:list")
  @GetMapping("/list")
  public TableDataInfo list(String ipaddr, String userName) {
    Collection<String> keys = StpUtil.searchSessionId("", 0, 10, true);
    if (CollUtil.isEmpty(keys)) {
      return new TableDataInfo();
    }
    var map = redissonClient.getBuckets().get(keys.toArray(new String[0]));
    if (CollUtil.isEmpty(map)) {
      return new TableDataInfo();
    }
    List<SysUserOnline> userOnlineList = new ArrayList<>();
    for (var value : map.values()) {
      var session = (SaSession) value;
      LoginUser user = (LoginUser) session.get(SaSession.USER);
      if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
        userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
      } else if (StringUtils.isNotEmpty(ipaddr)) {
        userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
      } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
        userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
      } else {
        userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
      }
    }
    Collections.reverse(userOnlineList);
    userOnlineList.removeAll(Collections.singleton(null));
    return getDataTable(userOnlineList);
  }

  /** 强退用户 */
  @SaCheckPermission("monitor:online:forceLogout")
  @Log(title = "在线用户", businessType = BusinessType.FORCE)
  @DeleteMapping("/{tokenId}")
  public AjaxResult forceLogout(@PathVariable String tokenId) {
    redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
    return success();
  }
}
